package io.gitee.zhangbinhub.admin.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.mybatisflex.annotation.Id
import com.mybatisflex.annotation.RelationManyToMany
import com.mybatisflex.annotation.Table
import io.gitee.zhangbinhub.admin.base.BaseTreeEntity
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.apache.commons.lang3.builder.EqualsBuilder
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.noear.snack4.annotation.ONodeAttr

@Table("t_menu")
@ApiModel("菜单信息")
data class Menu(
    @Id
    @ApiModelProperty(name = "菜单ID")
    var id: String = "",
    @ApiModelProperty(name = "应用ID")
    var appId: String = "",
    @ApiModelProperty(name = "菜单名称")
    var name: String = "",
    @ApiModelProperty(name = "菜单图标")
    var iconType: String? = null,
    @ApiModelProperty(name = "链接路径")
    var path: String? = null,
    @ApiModelProperty(name = "菜单是否启用")
    var enabled: Boolean = true,
    @ApiModelProperty(name = "是否可删除")
    var covert: Boolean = true,
    @ApiModelProperty(name = "链接打开模式；0-内嵌，1-新标签页")
    var openType: Int = 0,
    @ApiModelProperty(name = "序号")
    var sort: Int = 0,
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    @RelationManyToMany(
        joinTable = "t_role_menu_set", // 中间表
        joinSelfColumn = "menu_id",
        joinTargetColumn = "role_id"
    )
    var roleSet: MutableList<Role> = mutableListOf()
) : BaseTreeEntity<Menu>() {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        val menu = other as Menu
        return EqualsBuilder()
            .appendSuper(super.equals(other))
            .append(id, menu.id)
            .append(appId, menu.appId)
            .append(name, menu.name)
            .append(iconType, menu.iconType)
            .append(path, menu.path)
            .append(enabled, menu.enabled)
            .append(covert, menu.covert)
            .append(openType, menu.openType)
            .append(sort, menu.sort)
            .append(parentId, menu.parentId)
            .isEquals
    }

    override fun hashCode(): Int =
        HashCodeBuilder(17, 37)
            .appendSuper(super.hashCode())
            .append(id)
            .append(appId)
            .append(name)
            .append(iconType)
            .append(path)
            .append(enabled)
            .append(covert)
            .append(openType)
            .append(sort)
            .append(parentId)
            .toHashCode()

    override fun toString(): String =
        StringBuilder("Menu(")
            .append("id=$id")
            .append(",appId=$appId")
            .append(",name=$name")
            .append(",iconType=$iconType")
            .append(",path=$path")
            .append(",enabled=$enabled")
            .append(",covert=$covert")
            .append(",openType=$openType")
            .append(",sort=$sort")
            .append(",parentId=$parentId")
            .append(")")
            .toString()
}